package com.usesion.controller;


import com.usesion.base.entity.JsonResult;
import com.usesion.base.controller.BaseController;
import com.usesion.entity.Menu;
import com.usesion.entity.Role;
import com.usesion.service.MenuService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 菜单信息
 * 
 * @author XT
 */
@Controller
@RequestMapping("/system/menu")
public class MenuController extends BaseController {

    private String prefix = "system/menu";

    @Autowired
    private MenuService menuService;

    @RequiresPermissions("system:menu:view")
    @GetMapping()
    public String menu() {
        return prefix + "/menu";
    }

    @RequiresPermissions("system:menu:list")
    @GetMapping("/list")
    @ResponseBody
    public List<Menu> list() {
        List<Menu> menuList = menuService.selectMenuAll();
        return menuList;
    }

    /**
     * 删除菜单
     */
    @RequiresPermissions("system:menu:remove")
    @GetMapping("/remove/{menuId}")
    @ResponseBody
    public JsonResult remove(@PathVariable("menuId") Integer menuId) {
        if (menuService.selectCountMenuByParentId(menuId) > 0) {
            return JsonResult.failure("存在子菜单,不允许删除");
        }
        if (menuService.selectCountRoleMenuByMenuId(menuId) > 0) {
            return JsonResult.failure("菜单已分配,不允许删除");
        }
        if (menuService.deleteMenuById(menuId) > 0) {
            return JsonResult.success();
        }
        return JsonResult.failure();
    }

    /**
     * 修改菜单
     */
    @RequiresPermissions("system:menu:edit")
    @GetMapping("/edit/{menuId}")
    public String edit(@PathVariable("menuId") Integer menuId, Model model) {
        Menu menu = menuService.selectMenuById(menuId);
        model.addAttribute("menu", menu);
        return prefix + "/edit";
    }

    /**
     * 新增
     */
    @RequiresPermissions("system:menu:add")
    @GetMapping("/add/{parentId}")
    public String add(@PathVariable("parentId") Integer parentId, Model model) {
        Menu menu = null;
        if (parentId != null && !parentId.equals(0)) {
            menu = menuService.selectMenuById(parentId);
        } else {
            menu = new Menu();
            menu.setId(0);
            menu.setName("主目录");
        }
        model.addAttribute("menu", menu);
        return prefix + "/add";
    }

    /**
     * 保存菜单
     */
    @RequiresPermissions("system:menu:save")
    @PostMapping("/save")
    @ResponseBody
    public JsonResult save(Menu menu) {
        if (menuService.saveMenu(menu) > 0) {
            return JsonResult.success();
        }
        return JsonResult.failure();
    }

    /**
     * 选择菜单图标
     */
    @GetMapping("/icon")
    public String icon() {
        return prefix + "/icon";
    }

    /**
     * 校验菜单名称
     */
    @PostMapping("/checkMenuNameUnique")
    @ResponseBody
    public String checkMenuNameUnique(Menu menu) {
        String uniqueFlag = "0";
        if (menu != null) {
            uniqueFlag = menuService.checkMenuNameUnique(menu);
        }
        return uniqueFlag;
    }

    /**
     * 加载角色菜单列表树
     */
    @GetMapping("/roleMenuTreeData")
    @ResponseBody
    public List<Map<String, Object>> roleMenuTreeData(Role role) {
        List<Map<String, Object>> tree = menuService.roleMenuTreeData(role);
        return tree;
    }
    
    /**
     * 加载所有菜单列表树
     */
    @GetMapping("/menuTreeData")
    @ResponseBody
    public List<Map<String, Object>> menuTreeData(Role role) {
        List<Map<String, Object>> tree = menuService.menuTreeData();
        return tree;
    }
    
    /**
     * 选择菜单树
     */
    @GetMapping("/selectMenuTree/{menuId}")
    public String selectMenuTree(@PathVariable("menuId") Integer  menuId, Model model) {
        model.addAttribute("treeName", menuService.selectMenuById(menuId).getName());
        return prefix + "/tree";
    }
}